<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
    <title>修改商品信息</title>
    <!-- JQuery -->
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <!-- Bootstrap核心CSS和JS -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
        $(function () {
            $.getJSON(
                "/admin/goods/getEdit",
                {},
                function (json) {
                    if (json.statusCode !== 200) {
                        alert("获取失败！" + json.msg);
                    }
                    $("input[name='id']").attr("value", json.data.id);
                    $("input[name='name']").attr("value", json.data.name);
                    $("input[name='price']").attr("value", json.data.price);
                    $("input[name='num']").attr("value", json.data.num);
                    $("input[name='image']").attr("value", json.data.image);
                    $("input[name='categoryId']").attr("value", json.data.categoryId);
                },
                "json"
            );


            $("#btn_submit").click(function () {
                let url = $('#image').val();
                if (!isUrl(url))
                    $("#image").val(null);
                $.getJSON(
                    "/admin/goods/doEdit",
                    $("#goods").serialize(),
                    function (json) {
                        if (json.statusCode === 200) {
                            alert("修改成功");
                            location.href = "/admin/goods";
                            //正常响应
                        } else {
                            alert(json.msg);
                        }
                    },
                    "json"
                );
            })
        });

        /*  检验URl  */
        function isUrl(url) {
            let reg = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
            return reg.test(url);
        }
    </script>
</head>
<body>
<div class="container">
    <h1 class="page-header">编辑商品信息</h1>
    <form id="goods" class="form-horizontal" role="form">
        <div class="form-group">
            <label class="col-sm-2 control-label" for="id">
                商品ID：
            </label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="id" name="id" disabled>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label" for="name">
                商品名称：
            </label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="name">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label" for="price">
                商品价格：
            </label>
            <div class="col-sm-10">
                <input class="form-control" id="price" name="price" placeholder="纯数字，单位为分" type="number">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label" for="num">
                商品数量：
            </label>
            <div class="col-sm-10">
                <input class="form-control" id="num" name="num" placeholder="纯数字" type="number">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label" for="image">
                商品预览：
            </label>
            <div class="col-sm-10">
                <input class="form-control" id="image" name="image" placeholder="输入有效的URL连接" type="url">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label" for="categoryId">
                商品类别：
            </label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="categoryId" name="categoryId">
                <input type="button" class="btn btn-lg btn-primary btn-block" style="margin-top: 30px" id="btn_submit"
                       value="提交修改">
            </div>
        </div>
    </form>
</div>



</body>
</html>